using WebSMS.Core.ResponseEntity;

namespace WebSMS;

[Route("[controller]/[action]")]
[Controller]
public class DepartmentController : ControllerBase
{
    private readonly DepartmentService _departmentService;
    public DepartmentController(DepartmentService departmentService)
    {
        _departmentService = departmentService;
    }

    [HttpGet]
    public ResponseObject<List<DepartmentCollection>> GetDepartment()
    {
        return new ResponseObject<List<DepartmentCollection>>().Ok(_departmentService.GetDepartment());
    }
    
    [HttpPost]
    public async Task<ResponseObject<Department>> AddDepartment([FromBody]DepartmentForm department)
    {
       var status = await _departmentService.AddDepartment(new Department()
        {
            Id = Guid.NewGuid(),
            Name = department.Name,
            Description = department.Description,
            ParentId = department.ParentId
        });
        if (status)
        {
            return new ResponseObject<Department>().Ok("添加成功");
        }
        return new ResponseObject<Department>().Fail("添加失败");
    }

    [HttpPut]
    public async Task<ResponseObject<Department>> UpdateDepartment([FromBody] Department department)
    {
       var depart = await _departmentService.UpdateDepartment(new Department()
        {
            Id = department.Id,
            Name = department.Name,
            Description = department.Description,
            ParentId = department.ParentId
        });
        if (depart)
        {
            return new ResponseObject<Department>().Ok("修改成功");
        }
        return new ResponseObject<Department>().Fail("修改失败");
    }

    [HttpDelete("{id}")]
    public async Task<ResponseObject<Department>> DeleteDepartment(Guid id)
    {
        var status = await _departmentService.DeleteDepartment(id);
        if (status)
        {
            return new ResponseObject<Department>().Ok("删除成功");
        }
         return new ResponseObject<Department>().Fail("删除失败,该部门下子部门或人员无法删除");
    }


    [HttpGet]
    [Authorize]
    public async Task<ResponseObject<Object>> GetDepartmentDetail(Guid id)
    {
        var res = await _departmentService.GetDepartmentDetail(id);
        
        return new ResponseObject<Object>().Ok(res,"查询成功");
    }
}